#include "bsp_tm1650.h"
#include "esp_system.h"
#include "esp_log.h"
#include "string.h"
#include "bsp_param.h"
#include "bsp_beep.h"

static const char *TAG = "TM1650";
QueueHandle_t display_led_queue = NULL;
const uint8_t Conversion_Table[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
// void display_cmd_send()
static i2c_config_t conf_master = {
    .mode = I2C_MODE_MASTER,
    .sda_pullup_en = GPIO_PULLUP_DISABLE,
    .scl_pullup_en = GPIO_PULLUP_DISABLE,
    .master.clk_speed = 100000,
    .sda_io_num = I2C_MASTER_SDA_PORT,
    .scl_io_num = I2C_MASTER_SCL_PORT,
    .clk_flags = 0,
};

static void i2c_master_init()
{
    i2c_param_config(I2C_MASTER_NUM, &conf_master);
    i2c_driver_install(I2C_MASTER_NUM, conf_master.mode , 0, 0, 0);
    
}

static void TM1650_Wr_RAM(uint8_t Address, uint8_t Data)
{
    i2c_cmd_handle_t handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, Address, true);
    i2c_master_write(handle, &Data, 1, true);
    i2c_master_stop(handle);
    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 0);
    i2c_cmd_link_delete(handle);
}

static void TM1650_Display(uint8_t *display_buffer)
{
    uint8_t *tm1650_led = display_buffer;

    TM1650_Wr_RAM(LED_DIG1_ADD, tm1650_led[GRID1]);

    TM1650_Wr_RAM(LED_DIG2_ADD, tm1650_led[GRID2]);

    TM1650_Wr_RAM(LED_DIG3_ADD, tm1650_led[GRID3]);

    TM1650_Wr_RAM(LED_DIG4_ADD, tm1650_led[GRID4]);
}

static void tm1650_task(void* arg)
{
    uint8_t tm1650_key = 0;
    uint8_t led_display[GRID_MAX] = {0};
    sys_main_param_t* sys_tm1650_param = Get_sys_param_pos();
    for(;;){
        vTaskDelay(50 / portTICK_PERIOD_MS);
        /* 显示 */
        if (xQueueReceive(display_led_queue, led_display, 0)){
            TM1650_Display(led_display);
        }
        /* 按键 */
        tm1650_key = tm1650_key_scan();
        switch(tm1650_key){
            case KEY_DIG1_SW1_PRESS:
                ESP_LOGI(TAG,"启停\n");
                beep_output(BEEP_BUTTON);
                if(sys_tm1650_param->sys_status != SYS_RUN_NORMAL){
                    sys_tm1650_param->sys_status = SYS_RUN_NORMAL;
                }else{
                    sys_tm1650_param->sys_status = SYS_PAUSE;
                }
                break;
            
            case KEY_DIG1_SW2_PRESS:
                ESP_LOGI(TAG,"时间\n");
                beep_output(BEEP_BUTTON);
                if(sys_tm1650_param->sys_status != SYS_RUN_NORMAL){
                    if(sys_tm1650_param->sys_status == SYS_PAUSE){
                        sys_tm1650_param->sys_status = SYS_STOP;
                    }else{
                        if((++sys_tm1650_param->sys_runtime) > RUN_60MIN){
                            sys_tm1650_param->sys_runtime = RUN_5MIN;
                        }
                    }

                }
                break;

            case KEY_DIG1_SW3_PRESS:
                ESP_LOGI(TAG,"强度-\n");
                beep_output(BEEP_BUTTON);
                if(sys_tm1650_param->sys_status != SYS_RUN_NORMAL){

                    if(sys_tm1650_param->sys_power > POWER_1){
                        sys_tm1650_param->sys_power--;
                    }

                    if(sys_tm1650_param->sys_mode == MODE_DC){
                        if(sys_tm1650_param->sys_power > POWER_2)
                            sys_tm1650_param->sys_power = POWER_2;
                    }
                }
                break;

             case KEY_DIG1_SW4_PRESS:
                ESP_LOGI(TAG,"强度+\n");
                beep_output(BEEP_BUTTON);
                if(sys_tm1650_param->sys_status != SYS_RUN_NORMAL){

                    if(sys_tm1650_param->sys_power < POWER_8){
                        sys_tm1650_param->sys_power++;
                    }

                    if(sys_tm1650_param->sys_mode == MODE_DC){
                        if(sys_tm1650_param->sys_power > POWER_2)
                            sys_tm1650_param->sys_power = POWER_2;
                    }
                }
                break;
            default:
                break;
 
        }

    }

}

static void display_led_start()
{
    uint8_t led_start_buffer[GRID_MAX] = {0};
    memset(led_start_buffer, 0XFF, GRID_MAX);
    TM1650_Display(led_start_buffer);
    /* 开显示 */
    TM1650_Wr_RAM(0x48,0x07);
}

void tm1650_init()
{
    i2c_master_init();
    display_led_queue = xQueueCreate(5, GRID_MAX);
    xTaskCreate(&tm1650_task, "tm1650_task", 2048, NULL, 10, NULL);
    display_led_start();
}

uint8_t tm1650_key_scan()
{
    uint8_t Temp = KEY_NO_BUTTON_PRESS;
    static uint8_t Temp_old = KEY_NO_BUTTON_PRESS;
    i2c_cmd_handle_t handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, 0x4f, true);
    i2c_master_read(handle, &Temp, 1, I2C_MASTER_ACK);
    i2c_master_stop(handle);
    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 0);
    i2c_cmd_link_delete(handle);
    if(Temp == KEY_NO_BUTTON_PRESS){
        return KEY_NO_BUTTON_PRESS;
    }
 
    if(Temp != Temp_old){
        Temp_old = Temp;
        return Temp;
    }
    return KEY_NO_BUTTON_PRESS;

}




static uint8_t grid_buffer[GRID_MAX] = {0};
uint8_t *TM1650_led_datatrans(uint8_t data_status, uint16_t data_runtime_left, uint8_t mode,
                uint8_t strength,uint8_t ifble,uint8_t ifwifi,uint8_t index_flash_status)
{
    static uint16_t data_runtime_left_old = 0;
    static uint16_t strength_old = POWER_0;
    static union grid3_t grid3_temporary = {0};
    static union grid4_t grid4_temporary = {0};

    /* 倒计时 */
    if(data_runtime_left != data_runtime_left_old){
        grid_buffer[GRID1] = Conversion_Table[data_runtime_left/60/10];    // 分
        grid_buffer[GRID2] = Conversion_Table[data_runtime_left/60%10];
        data_runtime_left_old = data_runtime_left;
    }
    /* 蓝牙 */
    if(ifble >= BLUFI_BLE_CONNECT){
        grid4_temporary.grid4_bit_t.grid4_ble = 1;
    }else{
        grid4_temporary.grid4_bit_t.grid4_ble = 0;
    }

    /* wifi */
    if(ifwifi >= MQTT_APP_CONNECT){
        grid4_temporary.grid4_bit_t.grid4_wifi = 1;
    }else{
        grid4_temporary.grid4_bit_t.grid4_wifi = 0;
    }

    /* 闪烁状态 索引 */
    if(data_status == SYS_RUN_NORMAL){
        grid3_temporary.grid3_bit_t.grid3_point = (index_flash_status == FLASH_ON)?1:0;
    }else{
        grid3_temporary.grid3_bit_t.grid3_point = 1;
    }

    /* 强度 */
    if(strength_old != strength){
        if(mode == MODE_AC){
            if(strength <= POWER_4){
                grid4_temporary.grid4_bit_t.grid4_high = 0;
                grid3_temporary.grid3_bit_t.grid3_high = 0;
                grid4_temporary.grid4_bit_t.grid4_low = 1;
                grid3_temporary.grid3_bit_t.grid3_low = 1;
            }else{
                grid4_temporary.grid4_bit_t.grid4_high = 1;
                grid3_temporary.grid3_bit_t.grid3_high = 1;
                grid4_temporary.grid4_bit_t.grid4_low = 0;
                grid3_temporary.grid3_bit_t.grid3_low = 0;
            }
            switch(strength){
                case POWER_1:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 0;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 0;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_2:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 0;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_3:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_4:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_5:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_6:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_7:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_8:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 1;
                    break;
                default:
                    break;
            }
        }else{                          // DC
            if(strength < POWER_2){
                grid4_temporary.grid4_bit_t.grid4_high = 0;
                grid3_temporary.grid3_bit_t.grid3_high = 0;
                grid4_temporary.grid4_bit_t.grid4_low = 1;
                grid3_temporary.grid3_bit_t.grid3_low = 1;
            }else{
                grid4_temporary.grid4_bit_t.grid4_high = 1;
                grid3_temporary.grid3_bit_t.grid3_high = 1;
                grid4_temporary.grid4_bit_t.grid4_low = 0;
                grid3_temporary.grid3_bit_t.grid3_low = 0;
            }
            switch(strength){
                case POWER_1:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 0;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 0;
                    break;
                case POWER_2:
                    grid4_temporary.grid4_bit_t.grid4_power1 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power2 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power3 = 1;
                    grid4_temporary.grid4_bit_t.grid4_power4 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power5 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power6 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power7 = 1;
                    grid3_temporary.grid3_bit_t.grid3_power8 = 1;
                    break;
               
                default:
                    break;
            }
        }
        


    }

    grid_buffer[GRID3] = grid3_temporary.grid3_byte;
    grid_buffer[GRID4] = grid4_temporary.grid4_byte;

    return grid_buffer;
}








